﻿using System;
using System.Windows.Forms;
using Zhuang.WinForm.AppCode.Common;
using Zhuang.WinForm.AppCode.Enumeration;
using Zhuang.Utility.Common.DataBase;
using Zhuang.Utility.Common.Encryption;

namespace Zhuang.WinForm
{
    public partial class LoginSetting : Form
    {

        public LoginSetting()
        {
            InitializeComponent();
        }

        private void LoginSetting_Load(object sender, EventArgs e)
        {
            tbUserName.Text = SystemUtility.userName;

            tbServerName.Text = SystemUtility.serverName;
            tbDatabaseName.Text = SystemUtility.databaseName;
            tbDatabasePort.Text = SystemUtility.databasePort;
            tbDbUsername.Text = SystemUtility.dbUserName;
            tbDbUserPassword.Text = SystemUtility.dbUserPassword;


        }

        private void tp2_Ok_Click(object sender, EventArgs e)
        {

            LoginResult result = LoginResult.NO;

            result= checkUserPassword(tbUserName.Text.Trim(),tbOldUserPassword.Text.Trim());

            if (result == LoginResult.UserNotExists)
            {
                MessageBox.Show("用户名不存在！");
                return;
            }else if(result==LoginResult.PassWordWorng)
            {
                MessageBox.Show("旧密码错误！");
                return;
            }else if (tbNewUserPassword.Text.Trim() == "")
            {
                MessageBox.Show("新密码不能为空！");
                return;
            }
            else if (tbNewUserPassword.Text.Trim() != tbNewUserPasswordConfirm.Text.Trim())
            {
                MessageBox.Show("新密码确认错误！");
                return;
            }

            string newPassowrd = tbNewUserPassword.Text.Trim();

            try
            {
                SystemUtility.SaveAppSetting("UserPassword", newPassowrd);

                MessageBox.Show("保存成功！");

                SystemUtility.ReloadSettings();
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败！"+ex.Message);
            }

        }



        private LoginResult checkUserPassword(string username, string password)
        {
            LoginResult result = LoginResult.NO;

            Crypto ch = new Crypto();
            password = ch.GetEncryptedValue(password);


            if (username != SystemUtility.userName)
            {
                result = LoginResult.UserNotExists;
            }
            else if (password != SystemUtility.userPassword) 
            {
                result = LoginResult.PassWordWorng;
            }

            return result;
        }



        private void tp1_Ok_Click(object sender, EventArgs e)
        {
            try {
                Crypto ch = new Crypto();

                SystemUtility.SaveAppSetting("ServerName", tbServerName.Text.Trim());
                SystemUtility.SaveAppSetting("DatabaseName", tbDatabaseName.Text.Trim());
                SystemUtility.SaveAppSetting("DatabasePort", tbDatabasePort.Text.Trim());
                SystemUtility.SaveAppSetting("DbUserName",tbDbUsername.Text.Trim());

                string dbUserPassowrd = ch.GetEncryptedValue(tbDbUserPassword.Text.Trim());
                SystemUtility.SaveAppSetting("DbUserPassword", dbUserPassowrd);
                MessageBox.Show("保存成功！");

                SystemUtility.ReloadSettings();
                SqlHelper.connectionString = SystemUtility.connectionString;
            }
            catch (Exception ex)
            {
                MessageBox.Show("保存失败！"+ex.Message);
            }


        }

        private void tp1_Test_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    SqlHelper.ExecuteSqlByTime("getdate()",1);
            //    MessageBox.Show("连接成功!");
            //}
            //catch (Exception ex)
            //{ 
            //    MessageBox.Show("连接失败!"+ex.Message);
            //}
        }

    }
}
